package com.jeethink.erp.type.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.jeethink.common.constant.UserConstants;
import com.jeethink.common.core.domain.TreeSelect;
import com.jeethink.common.core.domain.entity.SysDept;
import com.jeethink.common.core.domain.entity.TbShop;
import com.jeethink.common.exception.CustomException;
import com.jeethink.common.utils.DateUtils;
import com.jeethink.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jeethink.erp.type.mapper.TbCommodityTypeMapper;
import com.jeethink.common.core.domain.entity.TbCommodityType;
import com.jeethink.erp.type.service.ITbCommodityTypeService;

/**
 * 商品类型Service业务层处理
 * 
 * @author ypr
 * @date 2021-06-09
 */
@Service
public class TbCommodityTypeServiceImpl implements ITbCommodityTypeService 
{
    @Autowired
    private TbCommodityTypeMapper tbCommodityTypeMapper;

    /**
     * 查询商品类型
     * 
     * @param commodityTypeId 商品类型ID
     * @return 商品类型
     */
    @Override
    public TbCommodityType selectTbCommodityTypeById(Long commodityTypeId)
    {
        return tbCommodityTypeMapper.selectTbCommodityTypeById(commodityTypeId);
    }

    /**
     * 查询商品类型列表
     * 
     * @param tbCommodityType 商品类型
     * @return 商品类型
     */
    @Override
    public List<TbCommodityType> selectTbCommodityTypeList(TbCommodityType tbCommodityType)
    {
        return tbCommodityTypeMapper.selectTbCommodityTypeList(tbCommodityType);
    }

    /**
     * 新增商品类型
     * 
     * @param tbCommodityType 商品类型
     * @return 结果
     */
    @Override
    public int insertTbCommodityType(TbCommodityType tbCommodityType)
    {
        tbCommodityType.setCreateTime(DateUtils.getNowDate());

        TbCommodityType info = tbCommodityTypeMapper.selectTbCommodityTypeById(tbCommodityType.getParentId());
        if(info == null ){
            tbCommodityType.setAncestors("0");
        }else {
            tbCommodityType.setAncestors(info.getAncestors() + "," + tbCommodityType.getParentId());
        }
        return tbCommodityTypeMapper.insertTbCommodityType(tbCommodityType);
    }

    /**
     * 修改商品类型
     * 
     * @param tbCommodityType 商品类型
     * @return 结果
     */
    @Override
    public int updateTbCommodityType(TbCommodityType tbCommodityType)
    {
        tbCommodityType.setUpdateTime(DateUtils.getNowDate());
        return tbCommodityTypeMapper.updateTbCommodityType(tbCommodityType);
    }

    /**
     * 批量删除商品类型
     * 
     * @param commodityTypeIds 需要删除的商品类型ID
     * @return 结果
     */
    @Override
    public int deleteTbCommodityTypeByIds(Long[] commodityTypeIds)
    {
        return tbCommodityTypeMapper.deleteTbCommodityTypeByIds(commodityTypeIds);
    }

    /**
     * 删除商品类型信息
     * 
     * @param commodityTypeId 商品类型ID
     * @return 结果
     */
    @Override
    public int deleteTbCommodityTypeById(Long commodityTypeId)
    {
        return tbCommodityTypeMapper.deleteTbCommodityTypeById(commodityTypeId);
    }



    @Override
    public List<TreeSelect> buildShopTreeSelect(List<TbCommodityType> tbCommodityTypes) {
        List<TbCommodityType> commodityTypesTrees = buildCommodityTypeTree(tbCommodityTypes);
        return commodityTypesTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public List<TbCommodityType> buildCommodityTypeTree(List<TbCommodityType> tbCommodityTypes) {
        List<TbCommodityType> returnList = new ArrayList<TbCommodityType>();
        List<Long> tempList = new ArrayList<Long>();
        for (TbCommodityType type : tbCommodityTypes)
        {
            tempList.add(type.getCommodityTypeId());
        }
        for (Iterator<TbCommodityType> iterator = tbCommodityTypes.iterator(); iterator.hasNext();)
        {
            TbCommodityType type = (TbCommodityType) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(type.getParentId()))
            {
                recursionFn(tbCommodityTypes, type);
                returnList.add(type);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = tbCommodityTypes;
        }
        return returnList;
    }

    @Override
    public List<TbCommodityType> selectTypeIds(Long commodityTypeId) {
        return tbCommodityTypeMapper.selectTypeIds(commodityTypeId);
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<TbCommodityType> list, TbCommodityType t)
    {
        // 得到子节点列表
        List<TbCommodityType> childList = getChildList(list, t);
        t.setChildren(childList);
        for (TbCommodityType tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }


    /**
     * 得到子节点列表
     */
    private List<TbCommodityType> getChildList(List<TbCommodityType> list, TbCommodityType t)
    {
        List<TbCommodityType> tlist = new ArrayList<TbCommodityType>();
        Iterator<TbCommodityType> it = list.iterator();
        while (it.hasNext())
        {
            TbCommodityType n = (TbCommodityType) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getCommodityTypeId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<TbCommodityType> list, TbCommodityType t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }



}
